<?php defined("MAIN_DIR") or die("No direct access");

function get_salary($number)
{
	$number = (string)$number;
	$length = strlen($number);
	
	$count = (int)($length / 3);
	if ($count > 0)
	{
		$new_num = "";
		$i = 0;
		$str = strrev($number);
		while ($i < $count)
			$new_num .= substr($str,($i++)*3,3)." ";
		$new_num .= substr($str,$count*3,$length - $count*3);
		
		return strrev(trim($new_num));
	}
	return $number;
}

function getValue($number)
{
	$number = (string)$number;
	$length = strlen($number);
	
	$key = 0;
	for($i = $length-1; $i > 0; $i--)
	{
		if ($number[$i] == "0")
			$key++;
		else
			break;
	}
	
	if ($key >= 3 && $key < 6)
		return substr($number,0,$length-3)." тыс.";
	elseif ($key >= 6 && $key < 9)
		return substr($number,0,$length-6)." млн.";
	else
		return $number;
}

function word_ending($count,$ending1,$ending2,$ending3,$with=true,$one=true)
{
	if ($count == 1)
		return $one ? $ending1 : "1 $ending1";
	$count = (string)$count;
	$char = (int)$count[strlen($count)-1];
	if ($char == 1)
		$word = $ending1;
	elseif ($char > 1 && $char < 5)
		$word = $ending2;
	elseif ($char >= 5 || $char == 0)
		$word = $ending3;
	
	// если число больше 10
	if (((int)$count)/10 > 1)
	{
		// работаем со строкой, т.к. $count может быть, например 112
		$char = (int)($count[strlen($count)-2].$count[strlen($count)-1]);
		if ($char > 10 && $char < 15)
			$word = $ending3;
	}
	
	return $with ? $count." ".$word : $word;	
}

function getRichText($source)
{
	global $current_city;
	
	$source = preg_replace("/{city}/",$current_city['city_name'],$source);
	$source = preg_replace("/{city_rod}/",$current_city['city_name_rod'],$source);
	$source = preg_replace("/{city_pred}/",$current_city['city_name_pred'],$source);
	$source = preg_replace("/\\n/","</p><p>",$source);
	
	return $source;
}

function getPeriod($months,$with=true)
{
	if ($months < 12)
		return $months." мес.";
	
	$years = (int)($months/12);
	
	if (($month = $months % 12) == 0)
		return $with ? word_ending($years,"год","года","лет",true,false) : $years;
	
	return word_ending($years,"год","года","лет",true,false)." $month мес.";
}

function to($url="/") {
	header("Location: $url");
	die;
}

// currency
function get_content($date)
{
	// загрузка
	$new_date = date("d/m/Y",strtotime($date));
	$fd = fopen("http://www.cbr.ru/scripts/XML_daily.asp?date_req=$new_date", "r");
	$text = "";
	if ($fd)
	{
		while (!feof ($fd))
			$text .= fgets($fd, 4096);
	}
	fclose ($fd);
	
	// обработка
	$rates = array();
	$pattern = "#<Valute ID=\"([^\"]+)[^>]+>[^>]+>([^<]+)[^>]+>[^>]+>[^>]+>[^>]+>[^>]+>[^>]+>([^<]+)[^>]+>[^>]+>([^<]+)#i";
	preg_match_all($pattern, $text, $out, PREG_SET_ORDER);
	foreach($out as $cur)
	{
		if($cur[2] == 840)
			$rates['dollar'] = number_format(str_replace(",",".",$cur[4]), 2);
		if($cur[2] == 978)
			$rates['euro'] = number_format(str_replace(",",".",$cur[4]), 2);
	}
	if (count($rates) != 0)
	{
		mysql_query("insert into `rates` values('".time()."','".date("Y-m-d",strtotime($date))."','$rates[dollar]','$rates[euro]')");
		return $rates;
	}
	
	return false;
}
function get_rates($date)
{
	$rates = array();
	$rates['date'] = $date;
	$today_res = mysql_query("select * from `rates` where `date` = '$date' limit 1");
	if (mysql_num_rows($today_res) > 0)
	{
		$today_row = mysql_fetch_array($today_res);
		$rates['dollar'] = $today_row['dollar'];
		$rates['euro'] = $today_row['euro'];
		return $rates;
	}
	else
	{
		$rates_from = get_content($date);
		if (count($rates_from) != 0)
		{
			$rates['dollar'] = $rates_from['dollar'];
			$rates['euro'] = $rates_from['euro'];
			return $rates;
		}
	}
	
	return false;
}
?>